package com.itany.shop.service.Proxy;


import com.itany.shop.entity.User;
import com.itany.shop.exception.*;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.tran.TransactionManager;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class UserServiceProxy implements UserService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private UserService userService = (UserService) ObjectFactory.getObject("userServiceTarget");

    @Override
    public void register(String username, String password, String phone, String address) throws RequestParameterErrorException, UserExistException {
        try {
            // 在执行核心业务逻辑之前,开启事务
            tran.begin();
            userService.register(username, password, phone, address);
            // 在执行核心业务逻辑之后,如果没有异常则提交事务
            tran.commit();
        } catch (RequestParameterErrorException e) {
            // 在交叉业务逻辑中只是进行了事务操作
            tran.rollback();
            // 对于异常,不做任何处理,怎么进来怎么出去
            throw e;
        } catch (UserExistException e) {
            tran.rollback();
            throw e;
        } catch (DataAccessException e) {
            tran.rollback();
            throw new ServiceException(e);
        }
    }

    @Override
    public User login(String username, String password) throws RequestParameterErrorException, UserNotExistException {
        try {
            tran.begin();
            User user = userService.login(username, password);
            tran.commit();
            return user;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (UserNotExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public User login(String username, String password, String code, String image) throws RequestParameterErrorException, UserNotExistException, CodeErrorException {
        try {
            tran.begin();
            User user = userService.login(username, password, code, image);
            tran.commit();
            return user;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (UserNotExistException e) {
            tran.rollback();
            throw e;
        } catch (CodeErrorException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyPassword(String password, String oldPassword, User user) throws RequestParameterErrorException, PasswordErrorException, UserNotExistException {
        try {
            tran.begin();
            userService.modifyPassword(password, oldPassword, user);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (PasswordErrorException e) {
            tran.rollback();
            throw e;
        } catch (UserNotExistException e) {
            tran.rollback();
            throw e;
        }
    }
}